package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.dao.mapper.EmployeeSchedulMapper;
import com.attendance.www.hr.dao.mapper.PunchTimeMapper;
import com.attendance.www.hr.dao.mapper.SchedulDayMapper;
import com.attendance.www.hr.entitys.EmployeeSchedul;
import com.attendance.www.hr.entitys.EmployeeSchedulExample;
import com.attendance.www.hr.entitys.PunchTime;
import com.attendance.www.hr.entitys.SchedulDayExample;
import com.attendance.www.hr.entitys.SchedulDayKey;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.ICommonService;
import com.attendance.www.hr.utils.TimeUtils;

@Service
public class CommonServiceImpl implements ICommonService {
	
	@Resource
	EmployeeSchedulMapper employeeSchedulDao;
	@Resource
	PunchTimeMapper punchTimeDao;
	@Resource
	SchedulDayMapper schedulDayDao;
	
	@Override
	public Integer getAttendanceStatus(Integer employeeId, Date attendanceDate,
			String startTime, String endTime) {
		Integer status = 1;
		//取得人员的排班时间
		EmployeeSchedulExample employeeSchedulExample = new EmployeeSchedulExample();
		employeeSchedulExample.or().andEmployeeIdEqualTo(employeeId).andFromDateLessThanOrEqualTo(attendanceDate).andThruDateGreaterThanOrEqualTo(attendanceDate);
		employeeSchedulExample.or().andEmployeeIdEqualTo(employeeId).andFromDateLessThanOrEqualTo(attendanceDate).andThruDateIsNull();
		employeeSchedulExample.setOrderByClause("from_date Desc");
		List<EmployeeSchedul> employeeSchedulList = employeeSchedulDao.selectByExample(employeeSchedulExample);
		if(employeeSchedulList==null || employeeSchedulList.size()==0){
			//未找到排班信息
			throw new ResultFaultException("5_00006"); 
		}
		
		SchedulDayExample sdExample = new SchedulDayExample();
		sdExample.createCriteria().andSdDateEqualTo(attendanceDate).andSwIdEqualTo(employeeSchedulList.get(0).getSwId());
		List<SchedulDayKey> schedulDayList = schedulDayDao.selectByExample(sdExample);
		//如果没查到数据，证明不上班。
		if(schedulDayList == null || schedulDayList.size()==0){
			return 0;
		}
		
		//取得打卡时间配置记录
		PunchTime punchTime = punchTimeDao.selectByPrimaryKey(employeeSchedulList.get(0).getPunchTimeId());
		//取得应出勤时长
		Integer punchMinutes = TimeUtils.differMinutes(punchTime.getStartFromTime(), punchTime.getEndFromTime());
		
		if(TimeUtils.compareTime(startTime, punchTime.getStartToTime())>0){
			if(TimeUtils.differMinutes(punchTime.getStartToTime(), startTime) > punchTime.getStartInterval()){
				//旷工
				status = 5;
			}else{
				//迟到
				status = 2;
			}
		}
		
		if(TimeUtils.compareTime(endTime, punchTime.getEndFromTime())<0){
			if(TimeUtils.differMinutes(endTime, punchTime.getEndFromTime()) > punchTime.getEndInterval()){
				//旷工
				status = 5;
			}else{
				if(status ==2){
					//迟到并早退
					status = 6;
				}else{
					//早退
					status = 3;
				}
			}
		}
		
		//判断工作时长
		if(TimeUtils.differMinutes(startTime, endTime) < punchMinutes && status ==1){
			//早退
			status = 3;
		}
		return status;
	}

}
